package com.cloudm.technician.ui.modules.cloudm_box.device_detail

import android.os.Bundle
import android.view.MenuItem
import com.cloudm.technician.R
import com.cloudm.technician.data.model.DeviceInfo
import com.cloudm.technician.extensions.addFragmentToActivity
import com.cloudm.technician.ui.base.BaseActivity

/**
 * 作者: wesley
 * 时间: 2017/12/19
 * 描述: 设备详情界面
 */
class DeviceDetailActivity : BaseActivity(), DeviceDetailFragment.OnTitleReceiveListener {

    private var flag: Int = 0
    private var info: DeviceInfo? = null
    private var fragment: DeviceDetailFragment? = null

    override val optionsMenuEnabled: Boolean
        get() = true

    override val menuRes: Int
        get() = R.menu.menu_device_detail

    override fun handleBundle(bundle: Bundle) {
        flag = bundle.getInt("flag")
        info = bundle.getParcelable("info")
    }

    override fun initVariables() {

    }

    override fun initViews(savedInstanceState: Bundle?) {
        fragment = manager.findFragmentById(
                R.id.common_content) as? DeviceDetailFragment ?: DeviceDetailFragment.newInstance(
                flag, info)
        fragment?.listener = this
        addFragmentToActivity(manager, fragment)
    }

    override fun initListener() {

    }

    override fun onTitleReceive(title: String) {
        headerTitleString = title
        displayHomeAsUpEnabled = true
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        return when (item?.itemId) {
            R.id.menu_next -> {
                fragment?.nextStep()
                true
            }
            else -> false
        }
    }

    override fun onBackPressed() {
        when (flag) {
            0 -> fragment?.showHintDialog()
            1 -> super.onBackPressed()
        }
    }
}